package com.springboot.activiti.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.springboot.activiti.model.LeaveModel;
import com.springboot.activiti.service.LeaveService;

@Controller
@RequestMapping({"/leave"})
public class LeaveController {
	
	@Resource(name="leaveService")
	private LeaveService leaveService;
	
	@RequestMapping("/toAdd")
	public String toAdd(){
		
		return "leave/add";
	}
	
	//保存请假信息并提交流程
	@RequestMapping("/saveAndSubmit")
	@ResponseBody
	public String saveAndSubmit(LeaveModel model, HttpSession session){
		
		//流程启动人的userId
		String initiator = (String) session.getAttribute("loginUser");
		model.setUserId(initiator);
		
		leaveService.saveAndSubmit(model);
		return "已提交申请";
	}
	
	//查看请假信息
	@RequestMapping("/view")
	public String view(String leaveId, String taskId, Model model){
		LeaveModel leaveModel = leaveService.selectById(leaveId);
		
		model.addAttribute("taskId", taskId);
		model.addAttribute("leaveModel", leaveModel);
		return "leave/view";
	}
	
	//销假
	@RequestMapping("/back")
	public String toBack(Model model, String leaveId, String taskId){
		LeaveModel leaveModel = leaveService.selectById(leaveId);
		
		model.addAttribute("taskId", taskId);
		model.addAttribute("leaveModel", leaveModel);
		return "leave/back";
	}
	
}
